package GUI;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

import order.ActionOrder;
import order.Order;
import order.UpgradeTechnologyLevel;
import model.Map;
import model.Player;
import model.TechnologyLevel;
import model.Territory;

public class TextPanel extends JPanel{
		
	    /**
	 * 
	 */
	private static final long serialVersionUID = -4422791170046625861L;
		private Player player;
		private Map map;
		private TextPanel textPanel;
		private MapPanel mapPanel;
		private ArrayList<Player> enemyList;
		
		private JPanel playerPanel, enemyPanel, historyPanel;
		
		private JLabel playerTitle, playerTerritoryandTroop, playerTechnologyandFood, playerTurn, playerTechnologyandFoodLevel;
		private JLabel playerSpy;
		private Clock playerTurnclock;
		private JButton playerUpgradeTechnologyLevel;
		private int turnNumber = 0;
		
		private JLabel[] enemyPanelInfo;
		private JScrollPane historyScroller;
		private ArrayList<JLabel> historyOrderLabelList;
		private ArrayList<JButton> historyCommitList;
		private ArrayList<JButton> historyUndoList;
		private ArrayList<Order> orderList;
		private JPanel historyContentPanel;
		
		public TextPanel(Player _player, Map _map){
			player = _player;
			map = _map;
			textPanel = this;
			enemyList = player.getEnemyList();
			initComponents();	
		}
		
		public void setMapPanel(MapPanel mapPanel){
			this.mapPanel = mapPanel;
		}
		
		private void initComponents(){
			this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
			
			//init player panel   
		    playerPanel =  new JPanel();
		    playerPanel.setLayout(new GridLayout(5, 2, 10, 10));	
		    playerPanel.setPreferredSize(new Dimension(this.getWidth(), 150));
			playerPanel.setBorder (BorderFactory.createTitledBorder(
						BorderFactory.createEtchedBorder (), "Player Information"));
		    
			playerTitle = new JLabel(player.getName() +  "'s Game: ");
			playerTerritoryandTroop = new JLabel("Territory: " + player.getTerritoryAmount() + "               Troop: " + player.getTroopAmount());
			playerTechnologyandFood = new JLabel("Technology:  " + player.getTechnology() + "               Food: " + player.getFood());
		  	playerTechnologyandFoodLevel = new JLabel("Technology Level: Infantry");
		  	playerUpgradeTechnologyLevel = new JButton("Upgrade Level");
		  	playerUpgradeTechnologyLevel.addActionListener(new upgradeTechnologyLevelListener());
			playerTurn = new JLabel("Turn 1");
			playerTurnclock = new Clock();
			playerSpy = new JLabel("Spy: 0");
			   
			playerPanel.add(playerTitle);
		    playerPanel.add(new JLabel("      "));
		    playerPanel.add(playerTurn);
		    playerPanel.add(playerTurnclock);
		    playerPanel.add(playerTerritoryandTroop);
		    playerPanel.add(playerTechnologyandFood);
		    playerPanel.add(playerTechnologyandFoodLevel);
		    playerPanel.add(playerUpgradeTechnologyLevel);
		    playerPanel.add(playerSpy);
			
		  	//init enemy panel
		    enemyPanel = new JPanel();
		    enemyPanelInfo = new JLabel[4];
		    enemyPanel.setLayout(new GridLayout(4, 1, 10, 10));
		    enemyPanel.setBorder (BorderFactory.createTitledBorder(
					BorderFactory.createEtchedBorder (), "Enemy Information"));
		    for (int i = 0; i < enemyList.size(); i++){
		    	enemyPanelInfo[i] =new JLabel(enemyList.get(i).getName() + ": " + enemyList.get(i).getLevel());
		        enemyPanel.add(enemyPanelInfo[i]);
		    }
		    //init history panel
		  	historyPanel = new JPanel();
		  	historyPanel.setLayout(new BoxLayout(historyPanel, BoxLayout.Y_AXIS));
		  	historyPanel.setPreferredSize(new Dimension(this.getWidth(), 450));
		  	historyPanel.setBorder (BorderFactory.createTitledBorder(
					BorderFactory.createEtchedBorder (), "Order History"));
		  	historyContentPanel = new JPanel();	
		  	historyContentPanel.setLayout(new BoxLayout(historyContentPanel, BoxLayout.Y_AXIS));
		  	
			historyScroller = new JScrollPane(historyContentPanel);
			historyScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
			historyScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		  	historyPanel.add(historyScroller);
		  	
		  	historyOrderLabelList = new ArrayList<JLabel> ();
		  	historyCommitList = new ArrayList<JButton> ();
		  	historyUndoList = new ArrayList<JButton> ();
		  	orderList = new ArrayList<Order>();
		  	 
		  	 //add sub panels
			this.add(playerPanel);
			this.add(enemyPanel);
			this.add(historyPanel);
		  }
		
		public void addHistory(Order order){
			  JPanel thePanel;
			  JLabel orderLabel;
			  JButton commit, undo;
			  
		      thePanel = new JPanel();
			  BoxLayout layout = new BoxLayout(this.getRootPane() ,BoxLayout.Y_AXIS);
			  getRootPane().setLayout(layout);
			  	  
			  orderLabel = new JLabel(order.printOrder());
			  commit = new JButton("Commit");
			  undo = new JButton("Undo");
			  historyOrderLabelList.add(orderLabel);
			  
			  historyCommitList.add(commit);
			  historyUndoList.add(undo);
			  orderList.add(order);
			  
			  thePanel.add(orderLabel);
			  thePanel.add(commit);
			  thePanel.add(undo);

			  historyContentPanel.add(thePanel);

			  commit.addActionListener(new commitListener(commit)); 
			  undo.addActionListener(new undoListener(undo));
			  
			 // client.addOrder(order);
		}
		

		public void updateBeforeTurn(){
			updatePlayerPanel();
			updateEnemyPanel();
			updateHistoryPanel();
		}
		
		public void updatePlayerPanel(){
			playerTerritoryandTroop.setText("Territory:" + player.getTerritoryAmount() + "               Troop:" + player.getTroopAmount());
			playerTechnologyandFood.setText("Technology:" + player.getTechnology() + "               Food:" + player.getFood());
            playerSpy.setText("Spy: " + player.getSpyAmount()); 	
		}
		
        public void updateEnemyPanel(){
        	for (int i = 0; i < 4; i++)
		    	enemyPanelInfo[i].setText(enemyList.get(i).getName() + ": " + enemyList.get(i).getLevel());
        }
        	
		private void updateHistoryPanel(){
        	historyContentPanel.removeAll();
    		historyContentPanel.revalidate();
    		historyContentPanel.repaint();
    		historyOrderLabelList.clear();
    		historyCommitList.clear();
    		orderList.clear();
        }
        
		class upgradeTechnologyLevelListener implements ActionListener{
		    public void actionPerformed(ActionEvent arg0) {
					UpgradeTechnologyLevel currentOrder = new UpgradeTechnologyLevel(player);			
				    if (currentOrder.checkOrder()){
				    	addHistory(currentOrder);
				    	currentOrder.executeOrder();
				    	playerTechnologyandFoodLevel.setText("Technology Level: " + player.getLevel());
				    	playerUpgradeTechnologyLevel.setEnabled(false);
				    	textPanel.updatePlayerPanel();
				    }
				}
		 }
			
		class commitListener implements ActionListener{
			private JButton button;
			public commitListener(JButton _button){
				button = _button;
			}
			public void actionPerformed(ActionEvent arg0) {
				  button.setEnabled(false);
				  int index = historyCommitList.indexOf(button);
				  historyUndoList.get(index).setEnabled(false);
				 // client.commitOrder(index);
				
			}
			
		}
		
		class undoListener implements ActionListener{
			private JButton button;
			public undoListener(JButton _button){
				button = _button;
			}
			public void actionPerformed(ActionEvent arg0) {
				  button.setEnabled(false);
				  int index = historyUndoList.indexOf(button);
				  historyCommitList.get(index).setEnabled(false);
				  Order order = orderList.get(index);
				  order.undoOrder();
				  
				  if (order instanceof UpgradeTechnologyLevel){
					  playerUpgradeTechnologyLevel.setEnabled(true);
					  textPanel.updatePlayerPanel();
				  }
				  else {
					  textPanel.updatePlayerPanel();
					  System.out.println(order.getSource().getOwner().getName());
					  System.out.println(order.printOrder());
					  mapPanel.getTerritoryPanel(order.getSource()).update();
				  }
				  if (order instanceof ActionOrder){
					  mapPanel.getTerritoryPanel(((ActionOrder) order).getDestination()).update();
				  }
				  }
			
		}
}
